/*
 * This servlet inserts chat messages or questions in the databsae
 */
package controllers.classes;

import classes.HibernateUtil;
import dao.*;
import models.*;


import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Achie
 */
public class ChatMessageController extends HttpServlet {

    public HibernateUtil hu = new HibernateUtil();
    ChatDao cd = new ChatDao(hu.openSession()); //open connection with the chatdao

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String message = request.getParameter("message"); //get the typed in message
        String chattype = request.getParameter("chattype"); //check the chattype (chat or question)

        //----------------------------------------------------------------------
        //Get the lesson object (to know in which lesson the chat belongs)
        //----------------------------------------------------------------------
        long lessonid = Long.parseLong(request.getParameter("lessonid")); //get the lesson id
        LessonDao ld = new LessonDao(hu.openSession());
        Lesson lesson = ld.get(lessonid);

        HttpSession ses = request.getSession();
        User usr = (User) ses.getAttribute("user"); //get the user object from the session
        if (isSpam(usr, lessonid)) { //spam method checker, if true=spam: stop executing the method.
            return;
        }

        //If it reaches this, it is no spam and the chat object is made and saved in the database.
        long chatid = System.nanoTime();
        Chat ch = new Chat(chatid, message, usr, lesson, chattype);

        cd.save(ch);
        hu.close();
    }

    //This method checks whether the user spams
    public boolean isSpam(User usr, long lessonid) {
        int floodSize = 5; //max size to post.

        //load last five posts.
        List<Chat> posts = cd.spamChecker(lessonid);

        //if the amount of posts in the chat is smaller than the flood, it is no spam (if true).
        if (posts.size() < floodSize) {
            return false; //stop executing the method.
        }

        long currentid = usr.getMember_id(); //get the current id of the member.

        for (int i = 0; i < floodSize; i++) { //do for the amount of the floodsize.
            //if the member did not post the last 5 messages, it is no spam.
            if (posts.get(i).getUser().getMember_id() != currentid) {
                return false;
            }
        }
        //if it reaches this end, it is spam.
        return true;

    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }
}
